<?php
SQLvalidatei$_GET['rec']);
SQLvalidate($_GET['user']);
SQLvalidate($_GET['cat'], 'varchar');

if(!perms_check('admin', 'logs'))
{
	redirect('index.php?module=error&error=auth_error');
	exit;
}

$fields=array('id', 'ip', 'date', 'who', 'text');

//sort
if($_GET['sort']!='ASC' && $_GET['sort']!='DESC')
$_GET['sort']='DESC';
if(!in_array($_GET['sortby'], $fields))
$_GET['sortby']='id';
if($_GET['sort']=='ASC')
$sortnext='DESC';
else
$sortnext='ASC';
//

//dla stron
if(!$_GET['rec'])
$_GET['rec']=0;
//


//delete old records from logs table 
$del_date=date("Y-m-d", strtotime ("-".$conf['log_days']." day")).' 00:00';
$db->query("DELETE FROM $conf[prefix]logs WHERE date < '$del_date'") or $db->err(__FILE__, __LINE__);
//

//create categories menu
$cat_menu=array();
$db->query("SELECT distinct cat FROM $conf[prefix]logs") or $db->err(__FILE__, __LINE__);
while($d=$db->fetch_object()){
	if($_GET['cat']==$d->cat) $ttl='<b>'.strtoupper($d->cat).'</b>';
	else $ttl=strtoupper($d->cat);
	$cat_menu[]='<a href="index.php?module=admin&amp;action=logs&amp;cat='.$d->cat.'&amp;user='.$_GET['user'].'">'.$ttl.'</a>';
}
//

$content='<p><a href="index.php?module=admin&amp;action=logs">'.$GLOBALS['lang']['global']['all'].'</a> '.implode(' ', $cat_menu).'</p>
<table border="0" width="100%" class="tabelka2">
<tr>
<td class="row_3" align="center"><a href="index.php?module=admin&amp;action=logs&amp;sortby=date&amp;sort='.$sortnext.'&amp;cat='.$_GET['cat'].'&amp;user='.$_GET['user'].'">'.$lang['admin_date'].'</a></td>
<td class="row_3" align="center"><a href="index.php?module=admin&amp;action=logs&amp;sortby=text&amp;sort='.$sortnext.'&amp;cat='.$_GET['cat'].'&amp;user='.$_GET['user'].'">'.$lang['admin_content'].'</a></td>
<td class="row_3" align="center" width="60"><a href="index.php?module=admin&amp;action=logs&amp;sortby=who&amp;sort='.$sortnext.'&amp;cat='.$_GET['cat'].'&amp;user='.$_GET['user'].'">'.$lang['admin_who'].'</a></td>
<td class="row_3" align="center"><a href="index.php?module=admin&amp;action=logs&amp;sortby=ip&amp;sort='.$sortnext.'&amp;cat='.$_GET['cat'].'&amp;user='.$_GET['user'].'">'.$lang['info'].'</a></td>
</tr>';

if($_GET['cat'])
$sql="WHERE cat='$_GET[cat]'";
else
$sql='';

	if($_GET['user']) {
		if($sql)
			$sql.="&& who=".$_GET['user'];
		else
			$sql="WHERE who=".$_GET['user'];

	}

$db->query("SELECT * FROM $conf[prefix]logs $sql") or $db->err(__FILE__, __LINE__);
$q=$db->num_rows();

$db->query("SELECT * FROM $conf[prefix]logs $sql ORDER by $_GET[sortby] $_GET[sort] LIMIT $_GET[rec], $conf[admin_per_page]") or $db->err(__FILE__, __LINE__);
while($d=$db->fetch_object())
{
	$i++;
	if($i>2)
		$i=1;

	if(is_numeric($d->who)) {
		$who=get_user_by_id($d->who);
		$who='<a href="index.php?module=admin&amp;action=logs&amp;cat='.$_GET['cat'].'&amp;user='.$who->id.'">'.$who->login.'</a><br />
			<span class="btn"><a href="index.php?module=privmsg&action=write&amp;user='.$who->id.'" title="'.$lang['pm'].' :: '.$who->login.'" target="_blank"><img src="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['pm'].'" alt="'.$lang['pm'].' :: '.$who->login.'" /></a></span> <span class="btn"><a href="index.php?module=users&amp;user='.$who->id.'" title="'.$lang['one_user_info'].'" target="_blank"><img src="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['user_info'].'" alt="'.$lang['one_user_info'].'" /></a></span>';
	}
	else
		$who=$lang['unknown_user'];

	$overLIB=overlib_text($d->info);	
	$content.='
		<tr>
		<td class="row_'.$i.'" align="center">'.date($conf['date_format'], strtotime($d->date)).'</td>
		<td class="row_'.$i.'" align="left">'.$d->text.'<br /><small><a href="'.$d->url.'" title="'.$d->url.'">'.add_dots_($d->url, 30).'</a></small></td>
		<td class="row_'.$i.'">'.$who.'</td>
		<td class="row_'.$i.'" align="center">
		<a href="http://www.ripe.net/perl/whois?&amp;searchtext='.$d->ip.'" target="_blank">'.$d->ip.'</a><br />
		'.string_template($lang['read_more'], array('url'=>'" onmouseover="overlib(\'<textarea class=\\\'button\\\' rows=15 cols=50>'.$overLIB.'</textarea>\', STICKY, MOUSEOFF, WRAP, CELLPAD, 5);" onmouseout="return nd();')).'
		</td>
		</tr>';

}

//page link
$page_link=split_to_pages('<a href="index.php?module=admin&amp;action=logs&amp;sortby='.$_GET['sortby'].'&amp;sort='.$_GET['sort'].'&amp;cat='.$_GET['cat'].'&amp;user='.$_GET['user'].'&amp;rec={rec}">{nr}</a>', $conf['admin_per_page'], $q, $_GET['rec']);

$content.='</table>
<p><a href="index.php?module=admin&amp;action=logs">'.$GLOBALS['lang']['global']['all'].'</a> '.implode(' ', $cat_menu).'</p>
<p>'.$page_link.'</p>
';

$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['admin_log']));
?>
